<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre-wrap;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../../three.js-r151/build/three.module.js",
		"three/addons/": "../../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        tween让动画更简洁
        <a href="https://github.com/tweenjs/tween.js/blob/main/docs/user_guide.md">tween readme - github</a>
    </div>
    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import TWEEN from 'three/addons/libs/tween.module.js';

        // 1. 必须创建的三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(3, 3, 3)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 2. 设置相机控件轨道控制器OrbitControls
        new OrbitControls(camera, canvasDom);

        // 3. 创建动画
        // 1. 位置改变
        console.log("cube.position", cube.position);
        let tweenA = new TWEEN.Tween(cube.position)
            .to({ x: 3, z:0 }, 3000)
            .start();

        let tweenB = new TWEEN.Tween(cube.position)
            .to({ x: 0, z: 3 }, 3000);

        tweenA.chain(tweenB)
        // tweenB.chain(tweenA)

        // 2. 颜色变化
        // 颜色初始值貌似不好设置呢？
        // new TWEEN.Tween(material.color)
        //     .to({r: 0, g: 0, b: 1}, 3000)
        //     .delay(2000)
        //     .start()
        //     .repeat(Infinity);

        // 颜色初始值设置
        console.log(material.color);
        new TWEEN.Tween({r: 1, g: 0, b: 0})
            .to({r: 0, g: 0, b: 1}, 3000)
            .delay(2000)
            .start()
            .onUpdate(obj => {
                material.color.setRGB(obj.r, obj.g, obj.b)
            });


        function render() {
            renderer.render(scene, camera);
            requestAnimationFrame(render)

            TWEEN.update(); //tween更新
        }
        render();
    </script>
</body>

</html>